package com.zn.bitmapDisplay
{

    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.DisplayObject;
    import flash.display.DisplayObjectContainer;
    import flash.display.FrameLabel;
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.display.Stage;
    import flash.events.Event;
    import flash.geom.Matrix;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.utils.Dictionary;
    import flash.utils.getDefinitionByName;
    import flash.utils.getQualifiedClassName;
    
    import mx.managers.SystemManager;
    
    import spark.primitives.Rect;

    public class BitmapDataFunc
    {
        public static var rootStage:Stage;

        private static var _createBitmapDataMCFrameCallBackDic:Dictionary = new Dictionary();

        /**
         *根据图形来绘制
         * @param array
         * @param className
         * @return
         *
         */
        public static function createBitmapDataByImage(array:Vector.<Class>, className:String):BitmapDataVO
        {
            var bitmapDataVO:BitmapDataVO = new BitmapDataVO(className);

            var bitmapDataClass:Class;

            var rect:Rectangle;
            var bitmapData:BitmapData;
            var bitmapData2:BitmapData;

            for (var i:int = 0; i < array.length; i++)
            {
                bitmapDataClass = array[i];

                //绘制对象，并却掉空白像素
                bitmapData = new bitmapDataClass();

                rect = bitmapData.getColorBoundsRect(0xFF000000, 0x000000, false);

                bitmapData2 = new BitmapData(rect.width, rect.height, true, 0);
                bitmapData2.draw(bitmapData, new Matrix(1, 0, 0, 1, -rect.x, -rect.y), null, null, new Rectangle(0, 0, rect.width, rect.height), true);

                bitmapDataVO.bitmapDataList.push(bitmapData2);
                bitmapDataVO.bitmapDataRectList.push(new Rectangle(0, 0, rect.width, rect.height));

                bitmapData.dispose();
            }

            return bitmapDataVO;
        }

        /**
         *根据MC来绘制对象
         * @param mc
         * @return
         *
         */
        public static function createBitmapDataByMC(mc:MovieClip, className:String = ""):BitmapDataVO
        {
            if (className == "")
                className = getQualifiedClassName(mc);

            var bitmapDataVO:BitmapDataVO = BitmapDataManager.getBitmapDataVO(className);
            if (bitmapDataVO)
                return bitmapDataVO;

            bitmapDataVO = new BitmapDataVO(className);

            var bitmapData:BitmapData;
            var rect:Rectangle;
            var frameLabel:FrameLabel;

            var dumPointSp:Sprite;

            for (var i:int = 1; i <= mc.totalFrames; i++)
            {
                mc.gotoAndStop(i);
                rect = mc.getBounds(mc);
                frameLabel = new FrameLabel(mc.currentFrameLabel, i);

                bitmapData = new BitmapData(rect.width, rect.height, true, 0);
                bitmapData.draw(mc, new Matrix(1, 0, 0, 1, -rect.x, -rect.y), null, null, null, true);

                bitmapDataVO.bitmapDataList.push(bitmapData);
                bitmapDataVO.bitmapDataRectList.push(rect);
                bitmapDataVO.currentLabels.push(frameLabel);
                bitmapDataVO.frameLabelDic[frameLabel.name] = frameLabel.frame;
            }

            return bitmapDataVO;
        }

        /**
         *根据类名称列表创建bitmapMC字典对象
         * @param classNameList
         * @return
         *
         */
        public static function createBitmapMCDic(classNameList:Array):Object
        {
            //bitmapMC字典 	,key:元件名	，value:bitmapMC
            var bitmapMCDic:Object = {};

            var bitmapMC:BitmapMovieClip;
            var bitmapDataVO:BitmapDataVO;
            var className:String;
            var mcClass:Class;
            var mc:MovieClip;
            for (var i:int = 0; i < classNameList.length; i++)
            {
                className = classNameList[i];

                mcClass = Class(getDefinitionByName(className));
                mc = new mcClass();
                bitmapDataVO = BitmapDataFunc.createBitmapDataByMC(mc);

                bitmapMC = new BitmapMovieClip(bitmapDataVO);
                bitmapMCDic[className] = bitmapMC;
            }

            return bitmapMCDic;
        }

        /**
         *根据MC来绘制对象,使用帧动态绘制
         * @param mc
         * @return
         *
         */
        public static function createBitmapDataByMCFrame(mc:MovieClip, className:String = "", callBack:Function = null,dynamicRect:Boolean=true):void
        {
            if (className == "")
                className = getQualifiedClassName(mc);

            var bitmapDataVO:BitmapDataVO = BitmapDataManager.getBitmapDataVO(className);
            if (bitmapDataVO)
            {
                if (callBack != null)
                    callBack(bitmapDataVO);

                return;
            }

            trace("createBitmapDataByMCFrame初始化：", className);

            var isCreating:Boolean = true;
            var callBackList:Array = _createBitmapDataMCFrameCallBackDic[className];
            if (callBackList == null)
            {
                callBackList = _createBitmapDataMCFrameCallBackDic[className] = [];
                isCreating = false;
            }

            callBackList.push(callBack);

            if (isCreating)
                return;

            bitmapDataVO = new BitmapDataVO(className);

            var bitmapData:BitmapData;
            var rect:Rectangle;
            var frameLabel:FrameLabel;

            var dumPointSp:Sprite;

            var totalFrame:int = getMCMaxFrame(mc);
            var currentFrame:int = 1;

            var fun:Function;
            rootStage.addChild(mc);
            mc.visible = false;
			
			if(dynamicRect==false)
			{
				rect = mc.getRect(mc);
				
				if (rect.width == 0)
					rect.width = 1;
				
				if (rect.height == 0)
					rect.height = 1;
			}
			
            mc.addEventListener(Event.ENTER_FRAME, fun = function(event:Event):void
            {
				if(dynamicRect)
				{
					rect = mc.getRect(mc);
					
					if (rect.width == 0)
						rect.width = 1;
					
					if (rect.height == 0)
						rect.height = 1;
				}
				
                bitmapData = new BitmapData(rect.width, rect.height, true, 0);
                bitmapData.draw(mc, new Matrix(1, 0, 0, 1, -rect.x, -rect.y), null, null, null, true);

                bitmapDataVO.bitmapDataList.push(bitmapData);
                bitmapDataVO.bitmapDataRectList.push(rect.clone());

                //完成
                if (currentFrame >= totalFrame)
                {
                    if (rootStage.contains(mc))
                        rootStage.removeChild(mc);

                    mc.removeEventListener(Event.ENTER_FRAME, fun);

                    for (var i:int = 0; i < callBackList.length; i++)
                    {
                        callBack = callBackList[i];

                        if (callBack != null)
                            callBack(bitmapDataVO);
                    }

                    delete _createBitmapDataMCFrameCallBackDic[className];

                    return;
                }

                currentFrame++;

            });
        }

        /**
         *获取MC的最大帧数
         * @param mc
         * @return
         *
         */
        private static function getMCMaxFrame(mc:MovieClip):int
        {
            var maxFrame:int = mc.totalFrames;

            var childMC:MovieClip;
            for (var i:int = 0; i < mc.numChildren; i++)
            {
                childMC = mc.getChildAt(i) as MovieClip;
                if (childMC)
                    maxFrame = Math.max(maxFrame, getMCMaxFrame(childMC));
            }

            return maxFrame;
        }

        /**
         *根据MC来绘制对象,使用帧动态绘制
         * @param mc
         * @param className
         * @return
         *
         */
        public static function createBitmapMCByMCFrame(mc:MovieClip, className:String = ""):BitmapMovieClip
        {
            var bitmapMC:BitmapMovieClip = new BitmapMovieClip();
            var mc:MovieClip;

            BitmapDataFunc.createBitmapDataByMCFrame(mc, className, function(bitmapDataVO:BitmapDataVO):void
            {
                bitmapMC.init(bitmapDataVO);
            });

            return bitmapMC;
        }

        /**
         *根据类名称列表创建bitmapMC字典对象
         * @param classNameList
         * @return
         *
         */
        public static function createBitmapDataMCFrameByDic(classNameList:Array, callBack:Function, progressCallBack:Function = null):void
        {
            var className:String;
            var mcClass:Class;
            var mc:MovieClip;

            var completeCount:int = 0;
            var totalCount:int = classNameList.length;

            for (var i:int = 0; i < totalCount; i++)
            {
                className = classNameList[i];

                try
                {
                    mcClass = Class(getDefinitionByName(className));
                }
                catch (error:Error)
                {
                    trace("未找定义:", className);
                    completeCount++;
                    continue;
                }

                mc = new mcClass();

                createBitmapDataByMCFrame(mc, className, function(value:BitmapDataVO):void
                {
                    completeCount++;

                    if (progressCallBack != null)
                        progressCallBack(mc, completeCount, totalCount);

                    trace("createBitmapDataMCFrameByDic初始化完成：", value.className);
                    if (completeCount >= totalCount && callBack != null)
                        callBack();
                });
            }
        }

        /**
         *bitmapData转换为动画
         * @param imageClassNameList bitmapData类名称，可以多图像绘制
         * @param rowRectList 每行开始切块的矩形位置
         * @param rowFrameCountList 每行帧数
         * @param classNemeList 每行转换为动画后的类名称
         * @return
         *
         */
        public static function createBitmapMCByImageSplit(splitVO:BitmapSplitInfoVO):BitmapMovieClip
        {
            var bitmapData:BitmapData = BitmapDataUtil.createBitmapDataByImages(splitVO.bitmapClassNameList);

			var oneBitmapData:BitmapData;
            var bitmapDataVO:BitmapDataVO;
			
			var rect:Rectangle=splitVO.rect.clone();
			
                bitmapDataVO = new BitmapDataVO(splitVO.className);
                for (var j:int = 0; j < splitVO.count; j++)
                {
					rect.x = rect.width * j;
                    oneBitmapData = new BitmapData(rect.width, rect.height, true, 0);
                    oneBitmapData.draw(bitmapData, new Matrix(1, 0, 0, 1, -rect.x, -rect.y), null, null, new Rectangle(0, 0, rect.width, rect.height), true);

                    bitmapDataVO.bitmapDataList.push(oneBitmapData);
                    bitmapDataVO.bitmapDataRectList.push(new Rectangle(0, 0, rect.width, rect.height));
                }
            bitmapData.dispose();	
			
			bitmapDataVO.keyFrameDic=splitVO.keyFrameDic;
			var bitmapMC:BitmapMovieClip=new BitmapMovieClip(bitmapDataVO);
			bitmapMC.frameRate=splitVO.frame;
			
            return bitmapMC;
        }
    }
}
